//
//  MoreView.m
//  享动
//
//  Created by darker on 16/4/8.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MoreView.h"

@interface MoreView ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

// 登录后的分区表头视图
@property (weak, nonatomic) IBOutlet UIView *loginHeadView;
// 登录后的分区表尾视图
@property (weak, nonatomic) IBOutlet UIView *loginFootView;
// 退出登录button
@property (weak, nonatomic) IBOutlet UIButton *exitButton;

// 分区表头视图
@property (weak, nonatomic) IBOutlet UIView *headView;

// 提示登录Label
@property (weak, nonatomic) IBOutlet UILabel *loginPrompt;

// 登录button
@property (weak, nonatomic) IBOutlet UIButton *toLogin;

// 每个row的label 箭头图标
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *rigthImageView;
@property (nonatomic, strong) UIImageView *rowImageView;

// 装载用户 头像昵称的View
@property (weak, nonatomic) IBOutlet UIView *userView;

// 用户头像
@property (weak, nonatomic) IBOutlet UIImageView *userHearImage;

// 用户昵称
@property (weak, nonatomic) IBOutlet UILabel *userName;



@end

@implementation MoreView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self = [[NSBundle mainBundle]loadNibNamed:@"more" owner:nil options:nil].firstObject;
        
        _tableView.frame = CGRectMake(0, 64, KWIDTH, KHEIGHT - 64);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

        _headView.hidden = YES;
        // 登录后
        _loginHeadView.frame = CGRectMake(0, 0, KWIDTH, 70);
        // 装载用户 头像昵称的View
        _userView.frame = CGRectMake(0, 0, KWIDTH - 100, 70);
        // 用户头像
        _userHearImage.frame = CGRectMake(10, 10, 50, 50);
        // 用户昵称
        _userName.frame = CGRectMake(70, 15, 150, 40);
        // 编辑个人资料
        _editData.frame = CGRectMake(KWIDTH - 90, 10, 80, 50);
        // 分区表尾
        _loginFootView.frame = CGRectMake(0, 0, KWIDTH, 50);
        // 退出登录button
        _exitButton.frame = CGRectMake(0, 0, KWIDTH, 50);
        _exitButton.layer.borderWidth = 0.1;
        _exitButton.layer.cornerRadius = 4.5;
        
        _userName.text = @"XXXX";
        _userName.textColor = [UIColor grayColor];
        _userHearImage.layer.cornerRadius = 25;
        _userHearImage.backgroundColor = [UIColor grayColor];
        
        [_exitButton addTarget:self action:@selector(exitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        _tableView.tableHeaderView = _loginHeadView;
        _tableView.tableFooterView = _loginFootView;
    }
    return  self;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 2;
    }
    else if (section == 1)
    {
        return 3;
    }
    else if (section == 2)
    {
        return 2;
    }
    return 0;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    _label = [[UILabel alloc] init];
    _rowImageView = [[UIImageView alloc] init];
    _rigthImageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH - 20, 15, 10, 15)];
    _rigthImageView.image = [UIImage imageNamed:@"下键2_u21"];
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
        else if (indexPath.row == 1)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
    }
    if (indexPath.section == 1)
    {
        if (indexPath.row == 0)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
        else if (indexPath.row == 1)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
        else if (indexPath.row == 2)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
    }
    if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
        else if (indexPath.row == 1)
        {
            [self setLabelText:@"XXXX" imageName:@"u40"];
        }
    }
    [cell addSubview:_label];
    [cell addSubview:_rigthImageView];
    [cell addSubview:_rowImageView];
    
    return cell;
}

#pragma mark --- 每个分区间间距 ---
- (CGFloat) tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section
{
    return 5.0;
}

#pragma mark --- 返回表尾与分区的间距 ---
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2)
    {
        return 5.0;
    }
    return 0;
}

#pragma mark --- 退出登录button触发方法 ---
- (void)exitButtonAction:(UIButton *)sender
{
    _headView.hidden = NO;
    _loginHeadView.hidden = YES;
    _loginFootView.hidden = YES;
    
    _headView.frame = CGRectMake(0, 0, KWIDTH, 150);
    // 立即登录button
    _toLogin.frame = CGRectMake(10, 80, KWIDTH - 20, 30);
    // 提示登录label
    _loginPrompt.frame = CGRectMake(0, 20, KWIDTH, 40);
    
    // 设置button边框粗细
    _toLogin.layer.borderWidth = 0.1;
    // 设置button圆角
    _toLogin.layer.cornerRadius = 4.5;
    _tableView.tableHeaderView = _headView;
}


- (void)setLabelText:(NSString *)text
             imageName:(NSString *)imageName
{
    _label.frame = CGRectMake(40, 5, 150, 40);
    _label.text = text;
    _label.textColor = [UIColor grayColor];
    _rowImageView.frame = CGRectMake(15, 18, 15, 15);
    _rowImageView.image = [UIImage imageNamed:imageName];
}

@end
